package wsi.exp.exp5;

import java.io.File;
import java.io.FilenameFilter;

import org.rygh.core.util.UIo;

import wsi.exp.ExpFolderController;
import wsi.function.Function;

public class JudgeFileManager {
	public final static String _JUDGED = "_judged";
	public final static FilenameFilter EXP2B_FILE;
	public final static FilenameFilter EXP2B_JUDGED_FILE;

	static {
		EXP2B_FILE = new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return name.startsWith("exp2b_") && name.endsWith(".xml");
			}
		};

		EXP2B_JUDGED_FILE = new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {
				return name.startsWith("exp2b_")
						&& name.endsWith(_JUDGED + ".txt");
			}
		};
	}

	private final Function function;
	private final File dir;

	public JudgeFileManager(Function function) {
		this.function = function;

		File edir = ExpFolderController.shared().getFolderForExp5();
		File sdir = new File(edir, function.getService());
		this.dir = new File(sdir, function.getName());
	}

	public Function getFunction() {
		return function;
	}

	public File[] getJudgedFiles() {
		return dir.listFiles(EXP2B_JUDGED_FILE);
	}

	public File getJudgeFile(String filename) {
		String fname = UIo.getFileNameWithoutExtension(filename) + _JUDGED;
		File jfile = null;

		for (File j : getJudgedFiles())
			if (j.getName().startsWith(fname)) {
				jfile = j;
				break;
			}

		return jfile;
	}

	public File[] getXmlFiles() {
		return dir.listFiles(EXP2B_FILE);
	}
}
